﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MinimizeEngine.Entities;
using MinimizeEngine.Managers;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace MinimizeEngine.Components.Render
{
    public class MELineRenderComponent : MEBaseComponent
    {

        #region Fields

        /// <summary>
        /// Line Vertices
        /// </summary>
        private VertexPositionColor[] mVertices;

        /// <summary>
        /// Vertex Buffer
        /// </summary>
        private VertexBuffer mVertexBuffer;

        /// <summary>
        /// Primitive Shapes Basic Effect
        /// </summary>
        private BasicEffect mBasicEffect;

        /// <summary>
        /// Scale information for the Render Component
        /// </summary>
        private Matrix mScaleMatrix;

        /// <summary>
        /// World Matrix for the Render Component
        /// </summary>
        private Matrix mWorldTransformMatrix;

        #endregion

        #region Properties


        #endregion

        #region Initialization

        /// <summary>
        /// Construct a new Render COmponent
        /// </summary>
        /// <param name="parent">Entity this will be attached to</param>
        public MELineRenderComponent(MEBaseEntity parent, Vector3 start, Vector3 end, Color color) : base(parent)
        {
            mVertices = new VertexPositionColor[2];
            mVertices[0] = new VertexPositionColor(start, color);
            mVertices[1] = new VertexPositionColor(end, color);

            mVertexBuffer = new VertexBuffer(MEEngineManager.DeviceManager.GraphicsDevice, VertexPositionColor.SizeInBytes * mVertices.Length, BufferUsage.None);
            mVertexBuffer.SetData<VertexPositionColor>(mVertices);

            mBasicEffect = new BasicEffect(MEEngineManager.DeviceManager.GraphicsDevice, null);
            mBasicEffect.LightingEnabled = true;
            mScaleMatrix = Matrix.CreateScale(Parent.Scale);
            mWorldTransformMatrix = mScaleMatrix * Parent.Rotation * Matrix.CreateTranslation(Parent.Position);
        }

        #endregion

        #region Update

        /// <summary>
        /// Update the Line Render Component
        /// </summary>
        /// <param name="gameTime"></param>
        public override void Update(GameTime gameTime)
        {
            mWorldTransformMatrix = mScaleMatrix * Parent.Rotation * Matrix.CreateTranslation(Parent.Position);
        }

        #endregion

        #region Draw
        
        public override void Draw()
        {
            mBasicEffect.World = mWorldTransformMatrix;
            mBasicEffect.View = MECameraManager.mActiveCamera.Parent.View;
            mBasicEffect.Projection = MECameraManager.mActiveCamera.Parent.Projection;
            mBasicEffect.VertexColorEnabled = true;

            // Begin effects
            mBasicEffect.Begin();
            {
                // Loop through each pass
                foreach (EffectPass pass in mBasicEffect.CurrentTechnique.Passes)
                {
                    pass.Begin();

                    // Draw the primitives
                    MEEngineManager.DeviceManager.GraphicsDevice.Vertices[0].SetSource(mVertexBuffer, 0, VertexPositionColor.SizeInBytes);
                    MEEngineManager.DeviceManager.GraphicsDevice.DrawPrimitives(PrimitiveType.LineList, 0, 1);

                    pass.End();
                }
            }
            mBasicEffect.End();
        }

        #endregion
    }
}
